import axios from 'axios';
import {Alert} from 'react-native';
import config from '../../config';

const instance = axios.create({
  baseURL: config.domain,
  timeout: 5000,
});

type Method = 'GET' | 'POST';

export interface PageVo<T> {
  pageNo: number;
  pageSize: number;
  count: number;
  pageCount: number;
  list?: T[];
}

export interface HttpConfig {
  needIntercept?: boolean; // 是否需要被拦截，此值设置为true时，错误提示信息，成功提示信息都将会被拦截
  needSuccessInfo?: boolean; // 是否需要"操作成功"提示
  showLoading?: boolean; // 是否需要loading
}

function request(
  method: Method,
  path: string,
  params: any,
  httpConfig?: HttpConfig,
): Promise<any> {
  return new Promise((resolve, reject) => {
    console.log('axios请求------------------')
    instance
      .request({
        url: path,
        method,
        data: params,
        headers: {
          'cookie-id': 'c48e6a666ecd06dac93f5a1b8c19070b',
        },
      })
      .then(res => {
        if (res.status === 200) {
          resolve(res.data.data);
        }
      })
      .catch(err => {
        console.log('失败', err);
        Alert.alert('警告', err);
        reject(err);
      });
  });
}

export function get(path: string, params: any, config?: any) {
  return request('GET', path, params);
}

export function post(path: string, params: any, config?: any) {
  return request('POST', path, params);
}
